package service

import (
	"common/rsa"
	"encoding/json"
	"fmt"
	"framework/game"
	"hall/pay/entity"
)

// GoodsPay 商城支付
func GoodsPay(money float64, orderNo string, UserCustId string, extension string, UserId string, UserName string, IdCard string) (string, error) {
	payMoney := fmt.Sprintf("%.2f", money) //开户金额
	divDetails := entity.DivDetails{
		//DivCustId:   config.Conf.Wallet.MerCustID,
		DivCustId: game.Conf.GameConfig["pay_wallet"]["value"].(map[string]any)["mer_cust_id"].(string),
		//DivAcctId:   config.Conf.Wallet.DivAcctID,
		DivAcctId:   game.Conf.GameConfig["pay_wallet"]["value"].(map[string]any)["div_acct_id"].(string),
		DivAmt:      payMoney,
		RiskDivType: 1,
	}
	var divDetailsArr []entity.DivDetails
	divDetailsArr = append(divDetailsArr, divDetails)
	divDetailsJson, _ := json.Marshal(divDetailsArr)
	objectInfo, _ := json.Marshal(entity.ObjectInfo{
		MarketType: 1,
	})
	data := entity.Pay{
		OrderId:    orderNo,
		UserCustId: UserCustId,
		DivType:    0,
		DivDetails: string(divDetailsJson),
		TransAmt:   payMoney,
		Extension:  extension,
		GoodsDesc:  "商品",
		ObjectInfo: string(objectInfo),
		UserName:   UserName,
		IdCardType: 10,
		IdCard:     IdCard,
		MerPriv:    UserId, //商户私有
	}
	data.Url = "hfpwallet/pay033"
	data = PayData(&data)
	Encrypt, err := rsa.PayVerifyRsa(&data)
	if err != nil {
		return "", err
	}
	response, err := PostData("hfPayRequest", Encrypt)
	if response.Code != 200 {
		err = fmt.Errorf(response.Msg)
		return "", err
	}
	HfpayOrderResult := JsonOrderResultData(response)
	if HfpayOrderResult.RespCode == "C00000" {
		return HfpayOrderResult.PayUrl, nil
	} else {
		err = fmt.Errorf(HfpayOrderResult.RespDesc)
		return "", err
	}
}
